#!/bin/bash
#
# CP2K (Intel/MKL x86_64) minimal arch file for Linux clusters
#
# Tested with: Intel(R) Fortran Intel(R) 64 Compiler Classic for applications running on Intel(R) 64, Version 2021.5.0

# Usage: Source this arch file after the module for Intel/MKL has been loaded if needed.
#
# Last update: 08.08.2022
#
# \
   if [[ "${0}" == "${BASH_SOURCE}" ]]; then \
      echo "ERROR: Script ${0##*/} must be sourced"; \
      echo "Usage: source ${0##*/}"; \
      exit 1; \
   fi; \
   this_file=${BASH_SOURCE##*/}; \
   make -j ARCH=${this_file%.*} VERSION=${this_file##*.} realclean; \
   make -j ARCH=${this_file%.*} VERSION=${this_file##*.}; \
   return

CC        = mpiicc
FC        = mpiifort
LD        = mpiifort
AR        = ar -r

CFLAGS    = -O2 -fopenmp -fp-model precise -funroll-loops -g -qopenmp-simd -traceback -xHost

DFLAGS    = -D__FFTW3
DFLAGS   += -D__MAX_CONTR=4
DFLAGS   += -D__MKL
DFLAGS   += -D__parallel
DFLAGS   += -D__SCALAPACK

FCFLAGS   = $(CFLAGS) $(DFLAGS)
FCFLAGS  += -diag-disable=8291
FCFLAGS  += -diag-disable=8293
FCFLAGS  += -fpp
FCFLAGS  += -free
FCFLAGS  += -I$(MKLROOT)/include
FCFLAGS  += -I$(MKLROOT)/include/fftw

LDFLAGS   = $(FCFLAGS) -static-intel -static_mpi
LDFLAGS_C = -nofor-main

MKL_LIB   = $(MKLROOT)/lib/intel64

LIBS      = $(MKL_LIB)/libmkl_scalapack_lp64.a
LIBS     += -Wl,--start-group
LIBS     += $(MKL_LIB)/libmkl_intel_lp64.a
LIBS     += $(MKL_LIB)/libmkl_sequential.a
LIBS     += $(MKL_LIB)/libmkl_core.a
LIBS     += $(MKL_LIB)/libmkl_blacs_intelmpi_lp64.a
LIBS     += -Wl,--end-group

# Required due to memory leak that occurs if high optimisations are used
mp2_optimize_ri_basis.o: mp2_optimize_ri_basis.F
	$(FC) -c $(subst O2,O0,$(FCFLAGS)) $<
